﻿using System;



namespace AnimalVoiceApplication

{

    // 动物基类

    abstract class Animal

    {

        public abstract void Voice();

    }



    // Cat类

    class Cat : Animal

    {

        public override void Voice()

        {

            Console.WriteLine("喵喵喵!");

        }

    }



    // Pig类

    class Pig : Animal

    {

        public override void Voice()

        {

            Console.WriteLine("哼哼哼!");

        }

    }



    // Dog类

    class Dog : Animal

    {

        public override void Voice()

        {

            Console.WriteLine("汪汪汪!");

        }

    }



    // 宠物店类

    class Store

    {

        // 根据传入的字符串返回相应的动物对象

        public Animal GetInstance(string animalType)

        {

            // 转换为小写以便不区分大小写

            switch (animalType.ToLower())

            {

                case "dog":

                    return new Dog();

                case "pig":

                    return new Pig();

                default:

                    return new Cat();

            }

        }

    }



    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("动物声音测试程序");

            Console.WriteLine("================");

            Console.WriteLine();



            // 创建宠物店实例

            Store petStore = new Store();



            // 测试获取不同动物并调用它们的Voice方法

            TestAnimal(petStore, "dog");

            TestAnimal(petStore, "pig");

            TestAnimal(petStore, "cat");

            TestAnimal(petStore, "rabbit"); // 不存在的动物类型，应该返回Cat



            // 测试用户输入

            Console.WriteLine("\n请输入动物类型 (dog/pig/cat 或其他): ");

            string userInput = Console.ReadLine();

            TestAnimal(petStore, userInput);



            Console.ReadLine();

        }



        // 辅助方法：测试特定动物类型

        static void TestAnimal(Store store, string animalType)

        {

            Animal animal = store.GetInstance(animalType);

            Console.Write($"{animalType} 的声音: ");

            animal.Voice();

        }

    }

}